import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/role_info.dart';
import 'package:winkai/ui/msg/controller/chat_controller.dart';
import 'package:winkai/ui/msg/widget/chat_bottom_bar.dart';
import 'package:winkai/ui/msg/widget/chat_title_bar.dart';
import 'package:winkai/utils/focus_utils.dart';
import 'package:winkai/widget/empty_layout.dart';

import 'widget/chat_history_widget.dart';

class ChatPage extends StatefulWidget {
  const ChatPage({super.key});

  @override
  State<ChatPage> createState() => _ChatPageState();
}

class _ChatPageState extends State<ChatPage> {
  late String tag;

  late ChatController controller;

  @override
  void initState() {
    super.initState();
    var args = Get.arguments;
    String sessionId = args['sessionId'];
    RoleInfo roleInfo = args['roleInfo'];
    tag = 'ChatController-$sessionId';
    controller = Get.put(
      ChatController(sessionId, roleInfo),
      tag: tag,
    );
  }

  @override
  void dispose() {
    Get.delete<ChatController>(tag: tag, force: true);
    FocusUtils.unFocus();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child: GetBuilder<ChatController>(
            tag: tag,
            builder: (_) {
              var emptyType = controller.emptyType;
              return Column(
                children: [
                  ChatTitleBar(controller: controller),
                  Expanded(
                    child: emptyType != null
                        ? EmptyLayout(type: emptyType)
                        : ChatHistoryWidget(controller: controller),
                  ),
                  ChatBottomBar(controller: controller),
                ],
              );
            }),
      ),
    );
  }
}
